from django.shortcuts import render, HttpResponse, redirect

from smart.utils.md5 import md5

from smart import models
from smart.views.form.create_equ import EquModelForm
from smart.views.form.create_homes import HomeModelForm
from smart.views.form.create_record import ReModelForm


def home(request):
    """
    主页，创建房间
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request, 'home/home.html', locals())


def create_home(request):
    """
    创建房间
    :param request:
    :return:
    """
    if request.method == 'GET':
        form = HomeModelForm()
        return render(request, 'home/create_home.html', locals())
    form = HomeModelForm(data=request.POST, files=request.FILES)
    # print(request.FILES)
    userinfo_id = request.session.get('user_dic').get('id')
    if form.is_valid():
        form.instance.userinfo_id = userinfo_id
        form.save()
        return redirect('home')
    return redirect('create_home')


def get_home(request):
    """
    查看房间
    :param request:
    :return:
    """
    if request.method == 'GET':
        userinfo_id = request.session.get('user_dic').get('id')
        query = models.Home.objects.filter(userinfo_id=userinfo_id)
        return render(request, 'home/get_home.html', locals())


def create_equ(request):
    """
    创建智能家居
    :param request:
    :return:
    """
    home_id = request.GET.get('home_id')
    if request.method == 'GET':
        form = EquModelForm()
        return render(request, 'home/create_home.html', locals())
    form = EquModelForm(data=request.POST, files=request.FILES)
    # print(request.FILES)
    # userinfo_id = request.session.get('user_dic').get('id')
    if form.is_valid():
        form.instance.home_id = home_id
        form.save()
        return redirect('get_home')
    return redirect('create_equ')


def get_equ(request):
    """
    查看房间
    :param request:
    :return:
    """
    if request.method == 'GET':
        userinfo_id = request.session.get('user_dic').get('id')
        query = models.Equipment.objects.filter(home__userinfo_id=userinfo_id)
        return render(request, 'home/get_equ.html', locals())


def edit_equ(request):
    """
    修改家电
    :param request:
    :return:
    """
    equ_id = request.GET.get('equ_id')
    equ_obj = models.Equipment.objects.filter(id=equ_id).first()
    if request.method == 'GET':
        form = EquModelForm(instance=equ_obj)
        return render(request, 'home/create_equ.html', locals())
    form = EquModelForm(data=request.POST, files=request.FILES, instance=equ_obj)
    userinfo_id = request.session.get('user_dic').get('id')
    if form.is_valid():
        form.instance.userinfo_id = userinfo_id
        form.save()
        return redirect('get_equ')
    return redirect('edit_equ')


def create_record(request):
    """
    上报记录
    :param request:
    :return:
    """
    equ_id = request.GET.get('equ_id')
    if request.method == 'GET':
        form = ReModelForm()
        return render(request, 'home/create_record.html', locals())
    form = ReModelForm(data=request.POST)
    obj = models.Equipment.objects.get(id=equ_id)
    if form.is_valid():
        form.instance.equ_id = equ_id
        form.instance.home_id = obj.home_id
        form.save()
        return redirect('get_equ')
    return redirect('get_equ')


def get_record(request):
    """
    查看
    :param request:
    :return:
    """
    if request.method == 'GET':
        userinfo_id = request.session.get('user_dic').get('id')
        query = models.Record.objects.filter(home__userinfo_id=userinfo_id)
        return render(request, 'home/get_record.html', locals())
